package Day09_Ex4_genericList;

// Shows basic use of the ArrayList and iterators 

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

// with the generics. 
public class demoList
{

    
    @SuppressWarnings("rawtypes")
	public static void main(String[] args)
    {
        // ** Create a List<String> 
        List<String> a = new ArrayList<String>();
        a.add("Don't");
        a.add("blame");
        a.add("me");

        // ** foreach -- iterate over collection easily 
        for (String str : a)
        {
            System.out.println(str);
        }

        // ** Instead of Iterator, make an Iterator<String> 
        Iterator<String> it = a.iterator();
        while (it.hasNext())
        {
            // NOTE: no cast required here -- it.next() is a String 
            String string = it.next();
            System.out.println(string);
        }

        // ** Likewise, can make a List<Integer> 
        List<Integer> ints = new ArrayList<Integer>();
        for (int i = 0; i < 10; i++)
        {
            ints.add(new Integer(i * i));
        }

        // No casts needed here -- it knows they are Integer 
        int sum = ints.get(5).intValue() + ints.get(6).intValue();
        System.out.println("Sum: " + sum);

        // With auto Unboxing, can just write it like this... 
        sum = ints.get(5) + ints.get(6);
        System.out.println("Sum: " + sum);

        // Can go back and forth between typed Collections and untyped "raw" 
        // forms -- may get a warning. 
        @SuppressWarnings("rawtypes")
		List<String> genList = new ArrayList(); // warning 
        List rawList = new ArrayList<String>(); // no warning 
        
        rawList.add("hello"); // warning 
        genList = rawList; // warning 
        rawList = genList; // no warning 


    }
}